/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package modelo;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.inject.Named;

/**
 *
 * @author Aluno
 */
@Named(value="imcMB")
@ManagedBean
@RequestScoped
public class ImcMB {

    /**
     * Creates a new instance of Imc
     */
    
    String urlws;
    String imc;
    String complemento;
    String altura;
    String peso;
    String resultado;

    public String getImc() {
        return imc;
    }

    public void setImc(String imc) {
        this.imc = imc;
    }

    public String getAltura() {
        return altura;
    }

    public void setAltura(String altura) {
        this.altura = altura;
    }

    public String getPeso() {
        return peso;
    }

    public void setPeso(String peso) {
        this.peso = peso;
    }

    public String getResultado() {
        return resultado;
    }

    public void setResultado(String resultado) {
        this.resultado = resultado;
    }
    

   
    
    
    
    public void buscarImc(){
        try {
            resultado = sendGet(urlws+"/IMC/"+altura+"/"+peso);
        } catch (Exception ex) {
            Logger.getLogger(ImcMB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public ImcMB() {
        
        urlws = "http://localhost:8080/BemEstarWS/webresources/BemEstar";
        
        try {
            imc = sendGet(urlws+"/IMC/1.75/75");
        } catch (Exception ex) {
            Logger.getLogger(ImcMB.class.getName()).log(Level.SEVERE, null, ex);
        }
               
    }
    
     private String sendGet(String url) throws Exception {
 
		
 
		URL obj = new URL(url);
		HttpURLConnection con = (HttpURLConnection) obj.openConnection();
 
		// optional default is GET
		con.setRequestMethod("GET");
 
		//add request header
		con.setRequestProperty("User-Agent", "Mozilla/5.0");
 
		int responseCode = con.getResponseCode();
		
 
		BufferedReader in = new BufferedReader(
		        new InputStreamReader(con.getInputStream()));
		String inputLine;
		StringBuffer response = new StringBuffer();
 
		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();
 
		//print result
		return response.toString();
 
	}
    
}

    

